/// [use]
// App.tsx
import React from 'react';
import { useZuoyIntl } from 'zuoy-hooks';

const Component = () => {
  const z = useZuoyIntl();
  return <div>{z('你好')}, world</div>;
};

export default Component;
/// [use]

/// [useZuoyIntl]
import { useIntl } from 'react-intl';

const useZuoyIntl = (env: 'dev' | 'prod') => {
  const intl = useIntl();
  const ev = env ?? localStorage.getItem('env');
  return (zuoyDescriptor: string | ZuoyDescriptor, values?: Record<string, PrimitiveType>) => {
    let params = null;
    let devValue = '';
    if (typeof zuoyDescriptor === 'string') {
      params = { id: zuoyKey(zuoyDescriptor) };
      devValue = formatMessage(zuoyDescriptor, values);
    } else {
      const { value, ...descriptor } = zuoyDescriptor;
      const valStr = `${value}`;
      params = { id: zuoyKey(valStr), ...descriptor };
      devValue = formatMessage(valStr, values);
    }
    return ev === 'prod' ? intl.formatMessage(params, values) : devValue;
  };
};
/// [useZuoyIntl]
